package com.gress.shop.controller.member;

import com.gress.shop.bo.Member;
import com.gress.shop.constants.EmailConstants;
import com.gress.shop.service.mail.IMailService;
import com.gress.shop.service.mail.impl.AbstractMailService;
import com.gress.shop.service.IMemberService;
import com.gress.shop.vo.SwalReturn;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api("会员注册")
@Controller
@RequestMapping("member")
public class RegisterController {
    @Resource
    private IMailService NTESMailServiceImpl;

    @Resource
    private IMemberService memberService;

    /**
     * @param
     * @Date: 15:38 2018/3/3
     * @Description 注册跳转页
     */
    @RequestMapping("register")
    public String register() {
        return "member/register";
    }

    /**
     * @param name
     * @param email
     * @Date: 21:45 2018/2/17
     * @Description 发送邮件
     */
    @ApiOperation("发送验证码")
    @RequestMapping("send_code")
    public String sendCode(@ModelAttribute("name") String name,
                           @ModelAttribute("email") String email,
                           HttpServletRequest request,
                           Model model) {
        HttpSession session = request.getSession();
        SwalReturn sendReturn = NTESMailServiceImpl.sendRegisterMailCode(email, EmailConstants.EMAIL_CODE_REGISTER, session);
        if (sendReturn.isSuccess() || EmailConstants.EMAIL_SEND.equals(sendReturn.getText())) {
            // 发送成功 和 邮件已经发送
            return "member/register2";
        } else {
            model.addAttribute("registerCode", sendReturn);
            return "member/register";
        }
    }

    /**
     * @param member
     * @param code   验证码
     * @Date: 22:27 2018/2/17
     * @Description 注册功能
     */
    @ResponseBody
    @RequestMapping("register_member")
    public SwalReturn registerMember(Member member,
                                     String code,
                                     HttpServletRequest request) {
        HttpSession session = request.getSession();
        SwalReturn swalReturn = memberService.registerMember(member, code, session);
        return swalReturn;
    }

    /**
     * @param
     * @Date: 14:54 2018/3/3
     * @Description: 成功注册后跳转
     */
    @RequestMapping("success_register")
    public String successRegister() {
        return "member/register3";
    }

}
